/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.driver.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;

public class BlobDBStatement implements Statement {

	private org.vnetcon.blobdb.database.Statement nativeStatement = null;
	private String rowid = null;
	protected ArrayList<String> batchStatements = new ArrayList<String>();
	
	public BlobDBStatement(org.vnetcon.blobdb.database.Statement nativeStatement){
		this.nativeStatement = nativeStatement;
	}
	
	@Override
	public boolean isWrapperFor(Class<?> arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public <T> T unwrap(Class<T> arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void addBatch(String bSQL) throws SQLException {
		this.batchStatements.add(bSQL);
	}

	@Override
	public void cancel() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void clearBatch() throws SQLException {
		this.batchStatements.clear();
	}

	@Override
	public void clearWarnings() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	/**
	 * This will not do anything. This is not throwing exceptions because this is
	 * probably called in many applications.
	 */
	@Override
	public void close() throws SQLException {
	}

	/**
	 * This will execute executeUpdate -method and returns always true.
	 */
	@Override
	public boolean execute(String bSQL) throws SQLException {
		this.executeUpdate(bSQL);
		return true;
	}

	@Override
	public boolean execute(String arg0, int arg1) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public boolean execute(String arg0, int[] arg1) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public boolean execute(String arg0, String[] arg1) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int[] executeBatch() throws SQLException {
		int iRet[] = {1};
		try {
			String strRet = this.nativeStatement.executeUpdates(this.batchStatements);
			if(strRet.indexOf("error:") > -1){
				throw new SQLException("BatchUpdate failed: " + strRet);
			}
		} catch (Exception e) {
			throw new SQLException(e.getMessage());
		}
		return iRet;
	}

	@Override
	public ResultSet executeQuery(String bSQL) throws SQLException {
		org.vnetcon.blobdb.database.ResultSet rs = null;
		BlobDBResultSet retRs = null;
		try {
			rs = this.nativeStatement.executeQuery(bSQL);
			if(!rs.getStatusMessage().startsWith("ok:")){
				throw new SQLException(rs.getStatusMessage());
			}
			retRs = new BlobDBResultSet(rs);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SQLException(e.getMessage());
		}
		return retRs;
	}

	/**
	 * This will always return 0 because BlobDB returns rowid instead of int value.
	 * You can get after execution BlobDB rowid by calling statements toString() -method.
	 */
	@Override
	public int executeUpdate(String bSQL) throws SQLException {
		try {
			this.rowid = this.nativeStatement.executeUpdate(bSQL);
			if(rowid.startsWith("error:")){
				throw new SQLException(rowid);
			}
		} catch (Exception e) {
			throw new SQLException(e.getMessage());
		}
		return 0;
	}

	@Override
	public int executeUpdate(String arg0, int arg1) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int executeUpdate(String arg0, int[] arg1) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int executeUpdate(String arg0, String[] arg1) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public Connection getConnection() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getFetchDirection() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getFetchSize() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public ResultSet getGeneratedKeys() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getMaxFieldSize() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getMaxRows() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public boolean getMoreResults() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public boolean getMoreResults(int arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getQueryTimeout() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public ResultSet getResultSet() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getResultSetConcurrency() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getResultSetHoldability() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getResultSetType() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public int getUpdateCount() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public SQLWarning getWarnings() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public boolean isClosed() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public boolean isPoolable() throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void setCursorName(String arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void setEscapeProcessing(boolean arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void setFetchDirection(int arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void setFetchSize(int arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void setMaxFieldSize(int arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void setMaxRows(int arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void setPoolable(boolean arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public void setQueryTimeout(int arg0) throws SQLException {
		throw new SQLException("Not implemented.");
	}

	@Override
	public String toString() {
		return this.rowid;
	}
	
}
